class Debounced {
    /**
     * @param func 函数
     * @param delay 延迟时间
     * @param immediate 是否默认执行一次(第一次不延迟)
     */
    public use = (func: Function, delay: number, immediate: boolean = false): Function => {
        let timer: number | undefined
        return (...args: any) => {
            if (immediate) {
                func.apply(this, args)
                immediate = false;
                return;
            }
            clearTimeout(timer);
            timer = setTimeout(() => {
                func.apply(this, args);
            }, delay)
        }
    }
}

export default Debounced;